#INCLUDE "RWMAKE.CH"

USER FUNCTION KOM190()
/*
--------------------------------------------------------------------------------------
OBJETIVO : Retornar para planilha excel o saldo de uma conta contabil (SINTETICA), 
             antes do zeramento das contas no periodo.
           Pois em 20/08/03, a funcao padrao do AP7/CTB, nao retorna o saldo de contas
           sinteticas.
--------------------------------------------------------------------------------------
*/
xPARAM := PARAMIXB
xDATA  := ctod(SUBSTR(xPARAM,1,8))
xCONTA := ALLTRIM(SUBSTR(xPARAM,9))

xSALDO:= 0
xSLD := 0
IF !EMPTY(xCONTA)
	CT1->(DBSETORDER(1))
	CT1->(DBSEEK(xFILIAL("CT1")+xCONTA,.T.))
	WHILE !CT1->(EOF()) .AND. xFILIAL("CT1") == CT1->CT1_FILIAL ;
		.AND. SUBSTR(CT1->CT1_CONTA,1,LEN(xCONTA)) == xCONTA
	 	IF CT1->CT1_CLASSE == "2"
 			xSLD := SaldoCT7(ct1->ct1_conta,xData,"01","1",'CTBXFUN',.T.,xData,'01') //FILIAL 01
 			xSALDO := xSALDO + xSLD[1]
			xSLD := SaldoCT7(ct1->ct1_conta,xData,"01","1",'CTBXFUN',.T.,xData,'02') //FILIAL 02
 			xSALDO := xSALDO + xSLD[1]
	 	ENDIF
 		CT1->(DBSKIP())
	END
ENDIF
RETURN(xSALDO)

*********************************************************
// FUNCAO PARA RETORNAR VALORES PARA VISOES GERENCIAIS 
// CLIENTE: REIVAX
// DESENVOLVIDO: OTAVIO - 12/06/2012 
// UTILIZADO NA VISAO "BALANCO PATRIMONIAL" (001)
*********************************************************

USER FUNCTION KOM190B()
/*
--------------------------------------------------------------------------------------
OBJETIVO : Retornar para planilha excel o saldo de uma conta contabil (SINTETICA), 
             antes do zeramento das contas no periodo.
--------------------------------------------------------------------------------------
*/
//xPARAM := PARAMIXB
If ALLTRIM(FUNNAME())$"CTBR510"
	xDATA  := MV_PAR08
ElseIf ALLTRIM(FUNNAME())$"CTBR500"
	xDATA  := MV_PAR09
Else
	xDATA  := STOD("20111231") //dDataBase
EndIf
xCONTA := "" //ALLTRIM(SUBSTR(xPARAM,9))  

xConta1  := alltrim(CTS->CTS_CT1INI)
xConta2  := alltrim(CTS->CTS_CT1FIM) 

xSALDO:= 0
xSLD := 0
IF !EMPTY(xCONTA1) .and. !EMPTY(xCONTA2) 

	//ALERT("KOM190 "+"Data: "+DTOC(xData)+" - "+xConta1+"-"+xConta2)

	CT1->(DBSETORDER(1))
	CT1->(DBSEEK(xFILIAL("CT1")+xCONTA1))
	WHILE !CT1->(EOF()) .AND. xFILIAL("CT1") == CT1->CT1_FILIAL .AND. alltrim(CT1->CT1_CONTA) <= alltrim(xCONTA2)
	 	IF CT1->CT1_CLASSE == "2"
 			xSLD := SaldoCT7(ct1->ct1_conta,xData,"01","1",'CTBXFUN',.T.,xData,'01') //FILIAL 01
 			xSALDO := xSALDO + xSLD[1]
			xSLD := SaldoCT7(ct1->ct1_conta,xData,"01","1",'CTBXFUN',.T.,xData,'02') //FILIAL 02
 			xSALDO := xSALDO + xSLD[1]
	 	ENDIF
 		CT1->(DBSKIP())
	END
ENDIF

//ALERT("SALDO: "+STR(xSaldo))

RETURN(xSALDO)